package com.example.mall.web.controller;

import com.example.mall.domain.MembershipLevels;
import com.example.mall.dto.MembershipLevelsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.MembershipLevelsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IMembershipLevelsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/membershipLevels")
public class MembershipLevelsController {

    @Autowired
    public IMembershipLevelsService membershipLevelsService;

    /**
     * 分页查询会员等级表信息
     * @param membershipLevelsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询会员等级表信息",notes = "分页查询会员等级表信息",httpMethod = "GET")
    public JSONResult queryPage(MembershipLevelsQuery membershipLevelsQuery){
        PageDTO<MembershipLevelsDto> membershipLevelsDtoPageDTO = membershipLevelsService.queryPage(membershipLevelsQuery);
        return JSONResult.success("查询成功",membershipLevelsDtoPageDTO);
    }

    /**
     * 根据id查询会员等级表信息
     * @param levelId
     * @return
     */
    @GetMapping("query/{levelId}")
    @ApiOperation(value = "根据id查询会员等级表信息",notes = "根据id查询会员等级表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("levelId")Long levelId){
        //1.根据id查询用户信息
        if (levelId == null){
            throw new GlobalCustomerException(ErrorEnumCode.LEVELID_NOT_ID);
        }
        MembershipLevels membershipLevels = membershipLevelsService.getById(levelId);
        return JSONResult.success("查询成功",membershipLevels);
    }

    /**
     * 根据id查询会员等级表信息
     * @param levelId
     * @return
     */
    @GetMapping("queryMembershipLevelsById/{levelId}")
    @ApiOperation(value = "根据id查询会员等级表信息",notes = "根据id查询会员等级表信息",httpMethod = "GET")
    public JSONResult queryMembershipLevelsById(@PathVariable("levelId")Long levelId){
        //1.根据id查询用户信息
        if (levelId == null){
            throw new GlobalCustomerException(ErrorEnumCode.LEVELID_NOT_ID);
        }
        MembershipLevels membershipLevels = membershipLevelsService.queryMembershipLevelsById(levelId);
        return JSONResult.success("查询成功",membershipLevels);
    }

    /**
     * 查询全部会员等级表信息
     * @param
     * @return
     */
    @GetMapping("queryMembershipLevelsList")
    @ApiOperation(value = "查询全部会员等级表信息",notes = "查询全部会员等级表信息",httpMethod = "GET")
    public JSONResult queryMembershipLevelsList(){
        List<MembershipLevels> membershipLevels = membershipLevelsService.queryMembershipLevelsList();
        return JSONResult.success("查询成功",membershipLevels);
    }

    /**
     * 新增会员等级表
     * @param membershipLevelsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增会员等级表",notes = "新增会员等级表",httpMethod = "POST")
    public JSONResult insert(@RequestBody MembershipLevelsDto membershipLevelsDto){
        membershipLevelsService.insert(membershipLevelsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改会员等级表信息
     * @param membershipLevelsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改会员等级表信息",notes = "修改会员等级表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody MembershipLevelsDto membershipLevelsDto){
        membershipLevelsService.update(membershipLevelsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除会员等级表信息
     * @param levelId
     * @return
     */
    @PostMapping("delete/{levelId}")
    @ApiOperation(value = "删除会员等级表信息",notes = "删除会员等级表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("levelId") Long levelId) {
        membershipLevelsService.delete(levelId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除会员等级表信息
     * @param levelIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除会员等级表信息",notes = "批量删除会员等级表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> levelIds) {
        membershipLevelsService.deleteBatch(levelIds);
        return JSONResult.success("批量删除成功");
    }
}
